package com.openapi.protocol.decode;

import com.openapi.data.JT808PackageData;
import com.openapi.data.JT808PushData;
import com.openapi.protocol.decode.subPackageHandle.handle.GpsHandle;
import com.openapi.protocol.decode.subPackageHandle.terminalPositionInfoUpload.TerminalPositionInfoUpload_0x0704;
import exsun.jt808.data.distribute_data.jt808.IssueOSPF;
import exsun.jt808.data.parseUtil.GpsUtil;
import exsun.jt808.data.upload_data.GpsStatusType;
import exsun.jt808.data.upload_data.Jt808EntityBase;
import exsun.jt808.data.upload_data.jt808.*;
import exsun.jt808.data.util.parse.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Component
public class Analyze8082019 extends Analyze808{

    @Autowired
    private TerminalPositionInfoUpload_0x0704 terminalPositionInfoUpload_0x0704;

    public JT808PushData terminalPositionInfoUploadDecoder(JT808PackageData packageData){
        return terminalPositionInfoUpload_0x0704.decode(packageData);
    }
    public Jt808EntityBase gpsDecoder(JT808PackageData packageData){
        //2019
        return GpsHandle.decode(packageData);
    }

    /**
     * 终端注册
     * @param packageData
     * @return
     */
    public Jt808EntityBase terminalRegisterDecoder(JT808PackageData packageData)
    {
        byte[] body = packageData.getBodyBuffers();
        TerminalRegister data = new TerminalRegister();
        data.setProvincialId(DecoderUtils.parseIntFromBytes(body, 0, 2));
        data.setCityId(DecoderUtils.parseIntFromBytes(body, 2, 2));
        data.setManufacturerId(DecoderUtils.parseStringFromBytes(body, 4, 11));
        data.setTerminalType(DecoderUtils.parseStringFromBytes(body, 15, 30));
        data.setTerminalId(DecoderUtils.parseStringFromBytes(body, 45, 30));
        data.setLicensePlateColor(DecoderUtils.parseIntFromBytes(body, 75, 1));
        data.setVehicleLicensePlate(DecoderUtils.parseStringFromBytes(body, 76, body.length - 76));
        return data;
    }

    /**
     * 终端登录鉴权
     * @param packageData
     * @return
     */
    public Jt808EntityBase terminalAuthenticationDecoder(JT808PackageData packageData)
    {
        byte[] body = packageData.getBodyBuffers();
        int n = body[0];
        TerminalAuthentication data = new TerminalAuthentication();
        //int codeLength = DecoderUtils.parseIntFromBytes(body,0,n);
        data.setAuthorizeCode(DecoderUtils.parseStringFromBytes(body, n, body.length));
        data.setIMEI(DecoderUtils.parseIntFromBytes(body,n+1,15));
        data.setSoftVersionId(DecoderUtils.parseIntFromBytes(body,n+16,20));
        return data;
    }

    /**
     * 查询终端属性应答
     * @param packageData
     * @return
     */
    public Jt808EntityBase terminalQueryPropResponseDecoder(JT808PackageData packageData)
    {
        byte[] body = packageData.getBodyBuffers();
        TerminalProperty data = new TerminalProperty();
        int type = DecoderUtils.parseIntFromBytes(body, 0, 2);
        data.setApplyPassengerVehicle(type & 0x01);
        data.setApplyDangerousVehicle((type & 0x02) >> 1);
        data.setApplyGeneralCargoVehicle((type & 0x04) >> 2);
        data.setApplyRentedOutVehicle((type & 0x08) >> 3);
        data.setHardDiskRecord((type & 0x40) >> 6);
        data.setModel((type & 0x80) >> 7);
        data.setTrailer((type & 0x100) >> 8);
        data.setTerminalManufacturerCode(DecoderUtils.parseStringFromBytes(body, 2, 5));
        data.setTerminalModel(DecoderUtils.parseStringFromBytes(body, 7, 30));
        data.setTerminalId(DecoderUtils.parseStringFromBytes(body, 37, 30));
        data.setICCID(DecoderUtils.parseBcdStringFromBytes(body, 67, 10));
        int n = body[77];
        data.setTerminalHardwareVersion(DecoderUtils.parseBcdStringFromBytes(body, 78, n));
        int m = body[78 + n];
        data.setTerminalFirmwareVersion(DecoderUtils.parseBcdStringFromBytes(body, 79 + n, m));
        int gnssProp = body[79 + n + m];
        data.setGpsLocationing(gnssProp & 0x01);
        data.setBdLocationing((gnssProp & 0x02) >> 1);
        data.setGlonassLocationing((gnssProp & 0x04) >> 2);
        data.setGalileoLocationing((gnssProp & 0x08) >> 3);
        int communicationProp = DecoderUtils.parseIntFromBytes(body, 80 + n + m, 1);
        data.setGprs(communicationProp & 0x01);
        data.setCdma((communicationProp & 0x02) >> 1);
        data.setTdScdma((communicationProp & 0x04) >> 2);
        data.setWcdma((communicationProp & 0x08) >> 3);
        data.setCdma2000((communicationProp & 0x10) >> 4);
        data.setTdLte((communicationProp & 0x20) >> 5);
        data.setOtherComunication((communicationProp & 0x80) >> 7);
        return data;
    }

    //查询区域或线路数据
    public static Jt808EntityBase queryAreaOrLineDataResponseDecoder(JT808PackageData packageData){
        byte[] body = packageData.getBodyBuffers();
        QueryAreaOrLineDataResponse data=new QueryAreaOrLineDataResponse();
        data.setQueryType(DecoderUtils.parseIntFromBytes(body, 0, 1));
        int count=DecoderUtils.parseIntFromBytes(body,1,1);
        List<Integer> mediaIds = new ArrayList<Integer>();
        int index = 4;
        for (int n = 0; n < count; n++)
        {
            mediaIds.add(DecoderUtils.parseIntFromBytes(body, index, 4));
            index += 4;
        }
        data.setLineIds(mediaIds);
        return data;
    }

    //查询服务器时间应答
    public static Jt808EntityBase queryServerTimeRequestDecoder(JT808PackageData packageData) {
        // 消息体为空
        return new QueryServerTimeRequest();
    }

    /*
	驾驶员身份信息采集上报
	 */
    public  Jt808EntityBase driverInfoUploadDecoder(JT808PackageData packageData)
    {
        byte[] body = packageData.getBodyBuffers();
        DriverInfo data = new DriverInfo();
        data.setStatus(DecoderUtils.parseIntFromBytes(body, 0, 1));
        data.setTime(DecoderUtils.parseBcdStringFromBytes(body, 1, 6));
        if (data.getStatus() == 1)
        {
            data.setIcCardreadResult(DecoderUtils.parseIntFromBytes(body, 7, 1));
            if (data.getIcCardreadResult() == 0)
            {
                int n = body[8];
                data.setDriverName(DecoderUtils.parseStringFromBytes(body, 9, n));
                data.setQualificationCode(DecoderUtils.parseStringFromBytes(body, 9 + n, 20));
                int m = body[29 + n];
                data.setIssuingAuthority(DecoderUtils.parseStringFromBytes(body, 30 + n, m));
                data.setCertificateValidity(DecoderUtils.parseBcdStringFromBytes(body, 30 + n + m, 4));
                data.setDriverId(DecoderUtils.parseStringFromBytes(body,34 + n + m, 20));
            }
        }
        // 存mongodb（驾驶员、车辆对应关系）
//        driverVehicleStatMongoService.insert(data, clientId);
//        statDriverIdDetailService.save(data, clientId);
        return data;
    }







}
